#include <QCoreApplication>
#include <QXmlStreamReader>
#include <QFile>
#include <QDebug>

int main(int argc, char *argv[])
{
    Q_UNUSED(argc);
    Q_UNUSED(argv);

    QFile file("/home/jake/Downloads/beamtool/tofd.icex");
    file.open(QIODevice::ReadOnly);
    {
        QXmlStreamReader xml(&file);
        QStringRef name;
        QXmlStreamAttributes attrs;

        while (!xml.atEnd() && !xml.hasError()) {
            switch( (int)xml.readNext() ) {
            case QXmlStreamReader::StartDocument:
            case QXmlStreamReader::EndDocument:
                break;
            case QXmlStreamReader::StartElement:
                name = xml.name();
                attrs = xml.attributes();
                if (!name.compare(QString("Material"))) {
                    qDebug() << "Piece"
                             << "\n Material:" << attrs.value("Name").toString()
                             << "\n Thickness:" << attrs.value("Thickness").toDouble() << "mm"
                             << "\n Length:" << attrs.value("Length").toDouble() << "mm"
                             << "\n Width:" << attrs.value("Width").toDouble() << "mm"
                             << "\n LW:" << attrs.value("Compression").toDouble() << "m/s"
                             << "\n SW:" << attrs.value("Shear").toDouble() << "m/s"
                             << " ";
                } else if (!name.compare(QString("Probe"))) {
                    qDebug() << "Group" << attrs.value("Number").toInt()
                             << "\n Type:" << attrs.value("Type").toString()
                             << "\n Name:" << attrs.value("Name").toString();
                } else if (!name.compare(QString("TOFDProbe"))) {
                    qDebug() << "Group" << attrs.value("Number").toInt()
                             << "\n Type:" << attrs.value("Type").toString()
                             << "\n Name:" << attrs.value("Name").toString()
                             << "\n PCS:" << attrs.value("PCS").toDouble()
                             << "\n Side Position:" << attrs.value("SidePosition").toDouble()
                             << "\n Refracted Angle:" << attrs.value("RefractedAngle").toDouble();
                } else if (!name.compare(QString("Transducer"))) {
                    qDebug() << "Probe"
                             << "\n Model:" << attrs.value("Name").toString()
                             << "\n Frequency:" << attrs.value("Frequency").toDouble() << "MHz"
                             << "\n Pri. Elements:" << attrs.value("Elements_X").toInt()
                             << "\n Sec. Elements:" << attrs.value("Elements_Y").toInt()
                             << "\n Pri. Pitch:" << attrs.value("Pitch_X").toDouble() << "mm"
                             << "\n Sec. Pitch:" << attrs.value("Pitch_Y").toDouble() << "mm"
                             << "\n Diameter:" << attrs.value("Diameter").toDouble() << "mm";
                } else if (!name.compare(QString("Wedge"))) {
                    qDebug() << "Wedge"
                             << "\n Model:" << attrs.value("Name").toString()
                             << "\n Velocity:" << attrs.value("Velocity").toDouble() << "m/s"
                             << "\n Angle:" << attrs.value("Angle").toDouble()
                             << "\n Roof Angle:" << attrs.value("RoofAngle", 0).toDouble()
                             << "\n First Element Height:" << attrs.value("Z").toDouble() << "mm"
                             << "\n Pri Offset:" << attrs.value("X").toDouble() << "mm"
                             << "\n Sec Offset:" << attrs.value("Yt").toDouble() << "mm"
                             << "\n Skew:" << (attrs.value("Skew").toInt() + 90)%360
                             << "\n Index Offset:" << attrs.value("HOff").toInt() << "mm"
                             << "\n Scan Offset:" << attrs.value("VOff").toInt() << "mm"
                             << "\n Length:" << attrs.value("X").toDouble() + attrs.value("Xt").toDouble() << "mm"
                             << "\n Width:" << attrs.value("Width").toDouble() << "mm"
                             << "\n Height:" << attrs.value("Height").toDouble() << "mm";
                } else if (!name.compare(QString("SectorialBeamset"))) {
                    double angleMin = attrs.value("AngleMin").toDouble();
                    double angleMax = attrs.value("AngleMax").toDouble();
                    double beamQty = attrs.value("NumBeams").toInt();
                    qDebug() << "Focallaw"
                             << "\n Name:" << attrs.value("Name").toString()
                             << "\n Velocity Mode:" << attrs.value("VelocityMode").toString()
                             << "\n Start Element:" << attrs.value("StartElement").toInt()
                             << "\n Aperture Elements:" << attrs.value("ApertureElements").toInt()
                             << "\n Angle Min:" << angleMin
                             << "\n Angle Max:" << angleMax
                             << "\n Angle Step:" << (angleMax-angleMin) / (beamQty >= 1 ? beamQty-1 : 1)
                             << "\n Focus Type:" << attrs.value("FocusType").toString()
                             << "\n Focus Length:" << attrs.value("FocusLength").toDouble()
                             << "\n Gate Start:" << attrs.value("GateStart").toDouble()
                             << "\n Gate Length:" << attrs.value("GateLength").toDouble();
                } else if (!name.compare(QString("LinearBeamset"))) {
                    int startElement = attrs.value("StartElement").toInt();
                    int aperture = attrs.value("ApertureElements").toInt();
                    int step = attrs.value("ElementStep").toInt();
                    int beams = attrs.value("NumBeams").toInt();
                    int stopElement = (beams-1)*step - 1 + aperture + startElement;

                    qDebug() << "Focallaw"
                             << "\n Name:" << attrs.value("Name").toString()
                             << "\n Velocity Mode:" << attrs.value("VelocityMode").toString()
                             << "\n Start Element:" << startElement
                             << "\n Stop Elements:" << stopElement
                             << "\n Element Step:" << step
                             << "\n Aperture:" << aperture
                             << "\n Refracted Angle:" << attrs.value("RefractedAngle").toDouble()
                             << "\n Focus Type:" << attrs.value("FocusType").toString()
                             << "\n Focus Length:" << attrs.value("FocusLength").toDouble()
                             << "\n Gate Start:" << attrs.value("GateStart").toDouble()
                             << "\n Gate Lenght:" << attrs.value("GateLength").toDouble();
                }
                break;
            case QXmlStreamReader::EndElement:
                //                qDebug("</%s>",xmlReader.name().toUtf8().data());
                break;
            case QXmlStreamReader::Characters:
                //                qDebug()<<xmlReader.text();
                break;
            }
        }
    }

    return 0;
}
